package com.note.chh.notebook;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.iflytek.cloud.SpeechUtility;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Uitls.RudenessScreenHelper;
import com.note.chh.notebook.dao.DaoMaster;
import com.note.chh.notebook.dao.DaoSession;
import com.tencent.bugly.Bugly;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by caihuahui on 2018/1/15.
 */

public class MyApplication extends Application{

    public static MyApplication instance;
    private Set<Activity> allActivities;
    private static DaoSession daoSession;
    public static String currentCity = "";//当前所在城市/区域
    public MyApplication getInstance() {
        if (instance==null) {
            instance = MyApplication.this;
        }
        return instance;
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        getInstance();
        //设计图标注的宽度
//        new RudenessScreenHelper(this, Constant.designWidth).activate();

        //okgo初始化
        OkGo.getInstance().init(this)                       //必须调用初始化
//                .setOkHttpClient(builder.build())               //建议设置OkHttpClient，不设置将使用默认的
                .setCacheMode(CacheMode.NO_CACHE)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)   //全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(3) ;                              //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0
//                .addCommonHeaders(headers)                      //全局公共头
//                .addCommonParams(params);                       //全局公共参数
        //讯飞语音初始化
        SpeechUtility.createUtility(MyApplication.this,"appid="+getString(R.string.app_id));
        //工具类初始化
        Utils.init(this);
        //greendao配置数据库
        setupDatabase();
        //Bugly 初始化  isDebug 是否开启debug模式，true表示打开debug模式，false表示关闭调试模式
        Bugly.init(getApplicationContext(), Constant.Bugly_AppID, Constant.Bugly_isDebug);
    }
    /**
     * 配置数据库
     */
    private void setupDatabase() {
        //创建数据库school.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "note.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }
    //添加activity
    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }
    //移除activity
    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }
    //退出app
    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
